/**
 * @author ASPa
*/
window.sls ? null : window.sls = {};
sls.Localisation = Class.create();
sls.Localisation.prototype = {
	allowRedirect: true,
	isNew: true,
	textId: '',
	translationField: null,
	mode: 'read',
	
	initialize: function() {
		sls.localisation = this;
		sls.localePanel = document.getElementById('localisationContainer');
		sls.localisation.translationField = document.getElementById('LocaleLocaleTranslation');
		sls.localisation.passwordBlock = document.getElementById('PasswordBlock');
		sls.localisation.selectLangsList = document.getElementById('localeSelect');
		sls.localisation.form = document.getElementById('LocaleForm');
		if (sls.localisation.selectLangsList)
			sls.localisation.selectedLangName = sls.localisation.selectLangsList.options[sls.localisation.selectLangsList.selectedIndex].text;
		else
			sls.localisation.selectedLangName = '';
		$('#LocaleForm').submit(function(){
			if (sls.localisation.mayCreate()) {
				sls.localePanel.style.display = 'block';
				$(this).ajaxSubmit({success: sls.localisation.update, url: this.action});
			}
			return false;
		});
		isStatic = false;
	},
	add: function($element) {
		if (sls.localisation.isNew) {
			sls.localisation.isNew = false;
			sls.localisation.show($element);
		}
		return false;
	},
	hide: function() {
		document.getElementById('localeTextId').value = '';
		sls.localisation.textId = '';
		sls.localePanel.style.display = "none";
		if (sls.modal)
			sls.modal.deactivate();
		sls.localisation.isNew = true;
		return false;
	},
	show: function($element) {
		sls.localisation.textId = $($element).attr('id');
		if (sls.modal) {
			sls.modal.show(sls.localePanel);
			sls.localePanel.style.display = "block";

			$('#LocaleTranslationLabel').text(sls.localisation.selectedLangName);
			$('#localeSource').text($($element).html());
			sls.localisation.translationField.value = "";
			document.getElementById('localeTextId').value = sls.localisation.textId;

			sls.modal.centrate()
			sls.modal.pull();
		}
		return false;
	},
	update: function(data) {
		var data = eval('('+data+')');
		$('form div.error-message').hide();
		if (!data.valid && data.messages) {
			for(var field in data.messages) {
				fieldName = '#Locale' + field.camelize();
				$(fieldName).parents('div.local_input').prepend('<div class="error-message">'+data.messages[field]+'</div>')
			}
			if (sls.modal)
				sls.modal.centrate()
		}
		if (data.valid && data.added) {
			els = document.getElementsByName(sls.localisation.textId);
			for (i = 0; i < els.length; i++) {
				if ( els[i].id == sls.localisation.textId)
				{
					els[i].innerHTML = sls.localisation.translationField.value;
				}
			}
			sls.localisation.passwordBlock.style.display = 'none';
			sls.localisation.hide();
		} else if(data.valid) {
			alert("Some error happend. You translation wasn't accepted.");
		}
	},
	setMode:function() {
		if (sls.localisation.mode == 'read')
		{
			sls.localisation.mode = 'write';
			sls.localisation.setEditable(true);
		} else {
			sls.localisation.mode = 'read';
			sls.localisation.setEditable(false);
		}
	},
	setEditable: function(set) {
		if (set) {
			$('span.local_text').css({background:'yellow'});
			$('span.local_text').click(function() {sls.localisation.add($(this)); return false;});
		} else {
			$('span.local_text').css({background:'none'});
			$('span.local_text').unbind("click");
		}
	},
	mayCreate: function() {
		return true;
	},
	changeLocale: function(langsList) {
		locale = langsList.options[langsList.selectedIndex].value;
		sls.localisation.selectedLangName = langsList.options[langsList.selectedIndex].text;
		if ( locale != '' ) {
			window.location = '/sls.php?act=change&locale='+locale;
		}
	}
}
$(function(){
	new sls.Localisation();	
});